<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" language="java" %>
<tiles:insertDefinition name="defaultTemplate">
    <tiles:putAttribute name="content">
        <header>
            <h2 class="page-title"><spring:message code="label.latest_books"/></h2>
            <a class="js-add-book add-book-link"
               <c:if test="${user!=null}">
                       style="display: inline-block"</c:if>
               href="#"><spring:message code="label.add_new_book"/></a>
        </header>
        <div class="main">
            <table class="books-table">
                <thead>
                <tr>
                    <th class="books-table-header books-table-cover">
                        <div class="main-cell"><spring:message code="label.cover"/></div>
                    </th>
                    <th class="books-table-header books-table-title">
                        <div class="main-cell"><spring:message code="label.book_title"/></div>
                    </th>
                    <th class="books-table-header books-table-author">
                        <div class="main-cell"><spring:message code="label.book_author"/></div>
                    </th>
                    <th class="books-table-header books-table-year">
                        <div class="main-cell"><spring:message code="label.year"/></div>
                    </th>
                    <th class="books-table-header books-table-uploader">
                        <div class="main-cell"><spring:message code="label.uploader"/></div>
                    </th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${books}" var="cell">
                    <tr>
                        <td class="books-table-cell books-table-cover"><img class="img-main"
                                                                            src="${contextPath}/resources/librry/books/covers/${cell.cover}">
                        </td>
                        <td class="books-table-cell books-table-title"><a
                                href="/librry/book/${cell.bookId}">${cell.title}</a>
                        </td>
                        <td class="books-table-cell books-table-author">${cell.author.firstName} ${cell.author.lastName}</td>
                        <td class="books-table-cell books-table-year">(<fmt:formatDate value="${cell.wrote}"
                                                                                       pattern="yyyy"/>)
                        </td>
                        <td class="books-table-cell books-table-uploader">${cell.user.username}</td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </tiles:putAttribute>
    <tiles:putAttribute name="popup">
        <form class="js-add-book-form add-book-form" action="/librry/add/book" method="post" enctype="multipart/form-data">
            <label class="add-book-field">
                <span class="add-book-label">Book title</span>
                <input class="add-book-input" name="book_title">
            </label>

            <label class="add-book-field">
                <span class="add-book-label choose-author">Choose Author</span>
                <select class="choose-book-field" id="select-author" name="author">
                    <c:forEach items="${authors}" var="author">
                        <option value="${author.authorId}">${author.firstName} ${author.lastName}</option>
                    </c:forEach>
                </select>
            </label>
            <label class="add-book-field">
                <span class="add-book-label create-author">Or Create a New One</span>
                <span class="add-book-label">First Name</span>
                <input class="add-book-input create-book-field" name="fname">
                <span class="add-book-label">Last Name</span>
                <input class="add-book-input create-book-field" name="lname">
            </label>
            <label class="add-book-field">
                <span class="add-book-label">Year</span>
                <input class="add-book-input" name="year" type="datetime">
            </label>
            <label class="add-book-field">
                <span class="add-book-label">Cover</span>
                <input class="add-book-input" name="cover" type="file">
            </label>
            <label class="add-book-field">
                <span class="add-book-label">File</span>
                <input class="add-book-input" name="book" type="file">
            </label>
            <button class="add-book-submit" type="submit">
                Add this book
            </button>
        </form>
    </tiles:putAttribute>
</tiles:insertDefinition>